package com.yesorno.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yesorno.constants.Constants;
import com.yesorno.model.HttpResponseObject;
import com.yesorno.ui.CustomTitleActivity;
import com.yesorno.ui.MyMenuList;
import com.yesorno.utils.HttpUtils;

public class UserInfoActivity extends CustomTitleActivity {

	private Button goBack;

	RegBtnsClickListeners rbc = new RegBtnsClickListeners();

	private long curUserId;

	private ImageView user_info_logo;

	private Button user_info_btn_attention;
	private Button user_info_btn_noattention;
	private Button user_info_btn_edit;
	private Button user_info_message;
	private Button user_info_add_blacklist;
	private Button user_info_manage_blacklist;

	private TextView user_info_name;
	private TextView user_info_sex;
	private TextView user_info_desc;
	private TextView user_info_public_topic_counts;
	private TextView user_info_attention_counts;
	private TextView user_info_join_topic_counts;
	private TextView user_info_fans_counts;

	private LinearLayout user_info_layout_publictopiccounts;
	private LinearLayout user_info_layout_attentioncounts;
	private LinearLayout user_info_layout_jointopiccounts;
	private LinearLayout user_info_layout_fanscounts;

	/**
	 * 注册页面按钮的监听类
	 * 
	 * @author samoin
	 * @date 2011-07-06
	 * @version 0.1
	 * 
	 */
	class RegBtnsClickListeners implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (v == goBack) {
				Intent intent = new Intent();
				UserInfoActivity.this.setResult(RESULT_OK, intent);
				UserInfoActivity.this.finish();
			}
			if (v == user_info_btn_edit) {
				Intent intent = new Intent();
				intent.setClass(UserInfoActivity.this,
						EditUserInfoActivity.class);
				startActivity(intent);
			}
			if (v == user_info_layout_publictopiccounts) {
				goEdit(user_info_layout_publictopiccounts);
				Intent intent = new Intent();
				intent.putExtra("userId", curUserId);
				intent.putExtra("topicType", "publish");
				intent.setClass(UserInfoActivity.this, TopicListActivity.class);
				startActivity(intent);
			}
			if (v == user_info_layout_attentioncounts) {
				goEdit(user_info_layout_attentioncounts);
				Intent intent = new Intent();
				intent.putExtra("type", "attention");
				intent.putExtra("userId", curUserId);
				intent.setClass(UserInfoActivity.this,
						AttentionPeopleActivity.class);
				startActivity(intent);
			}
			if (v == user_info_layout_jointopiccounts) {
				goEdit(user_info_layout_jointopiccounts);
				Intent intent = new Intent();
				intent.putExtra("userId", curUserId);
				intent.putExtra("topicType", "join");
				intent.setClass(UserInfoActivity.this, TopicListActivity.class);
				startActivity(intent);
			}
			if (v == user_info_layout_fanscounts) {
				goEdit(user_info_layout_fanscounts);
				Intent intent = new Intent();
				intent.putExtra("type", "fans");
				intent.putExtra("userId", curUserId);
				intent.setClass(UserInfoActivity.this,
						AttentionPeopleActivity.class);
				startActivity(intent);
			}
			if (v == user_info_manage_blacklist) {
				Intent intent = new Intent();
				intent.putExtra("curUserId", curUserId);
				intent.setClass(UserInfoActivity.this, BlackListActivity.class);
				startActivity(intent);
			}
		}
	}

	public void goEdit(LinearLayout layout) {
		resetLayoutChildTextViewColor(user_info_layout_publictopiccounts,
				R.color.white);
		resetLayoutChildTextViewColor(user_info_layout_attentioncounts,
				R.color.white);
		resetLayoutChildTextViewColor(user_info_layout_jointopiccounts,
				R.color.white);
		resetLayoutChildTextViewColor(user_info_layout_fanscounts,
				R.color.white);
		resetLayoutChildTextViewColor(layout, R.color.topic_select_yellow);
	}

	public void resetLayoutChildTextViewColor(LinearLayout layout, int colorId) {
		for (int i = 0; i < layout.getChildCount(); i++) {
			TextView t = (TextView) layout.getChildAt(i);
			t.setTextColor(getResources().getColor(colorId));
		}
	}

	@Override
	public void setTitleViews() {
		LinearLayout titleLayout = getTITLE_LAYOUT();
		// 返回按钮
		goBack = CustomTitleActivity.createTitleLeftButton(
				UserInfoActivity.this, R.string.userinfo_title_btn_goback,
				Constants.TITLE_BTN_MARGIN1);
		goBack.setOnClickListener(rbc);
		titleLayout.addView((View) goBack.getParent());
		// 中间的资料的文字
		TextView tv = CustomTitleActivity.createTitleDesc(
				UserInfoActivity.this, R.string.userinfo_title_common, 0);
		titleLayout.addView((View) tv.getParent());
		// 右边的站位的空字符串
		TextView tv2 = CustomTitleActivity.createTitleDesc(
				UserInfoActivity.this, 0, 0);
		titleLayout.addView((View) tv2.getParent());
	}

	@Override
	public void setLayoutId() {
		setContentView(R.layout.user_info);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		curUserId = getIntent().getLongExtra("currentUserId", 0);
		super.onCreate(savedInstanceState);
		((LinearLayout) findViewById(R.id.menus)).addView(new MyMenuList(
				UserInfoActivity.this, 2));

		setMyView();

		// TODO 获取该用户的信息
		HttpResponseObject hro = HttpUtils.getInfoByUrlPost(
				Constants.USER_INFO_URL + "userId=" + curUserId,
				UserInfoActivity.this);
		try {
			JSONObject jsonObject = hro.getResult().getJSONObject("user");
			String userName = jsonObject.getString("userName");
			String userLogo = jsonObject.getString("userLogo");
			String sex = jsonObject.getString("sex");
			String desc = jsonObject.getString("desc");
			int publicTopicCounts = jsonObject.getInt("publicTopicCounts");
			int attentionCounts = jsonObject.getInt("attentionCounts");
			int joinTopicCounts = jsonObject.getInt("joinTopicCounts");
			int fansCounts = jsonObject.getInt("fansCounts");
			Boolean isAttention = jsonObject.getBoolean("isAttention");

			Handler handler = new Handler();
			CustomTitleActivity.BitmapThread r = new CustomTitleActivity.BitmapThread(
					userLogo, user_info_logo, false);
			handler.post(r);
			user_info_name.setText(userName);
			user_info_sex.setText(Constants.USER_SEX_MAP.get(sex));
			user_info_desc.setText(desc);
			user_info_public_topic_counts.setText(publicTopicCounts + "");
			user_info_attention_counts.setText(attentionCounts + "");
			user_info_join_topic_counts.setText(joinTopicCounts + "");
			user_info_fans_counts.setText(fansCounts + "");
			if (curUserId != Constants.USER_ID) {// 别人
				user_info_add_blacklist.setVisibility(View.VISIBLE);
				if (isAttention) {
					user_info_btn_noattention.setVisibility(View.VISIBLE);
					user_info_message.setVisibility(View.VISIBLE);
				} else {
					user_info_btn_attention.setVisibility(View.VISIBLE);

				}
			} else {
				user_info_btn_edit.setVisibility(View.VISIBLE);
				user_info_manage_blacklist.setVisibility(View.VISIBLE);
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	private void setMyView() {
		user_info_logo = (ImageView) findViewById(R.id.user_info_logo);

		user_info_btn_attention = (Button) findViewById(R.id.user_info_btn_attention);
		user_info_btn_noattention = (Button) findViewById(R.id.user_info_btn_noattention);
		user_info_btn_edit = (Button) findViewById(R.id.user_info_btn_edit);
		user_info_btn_edit.setOnClickListener(rbc);
		user_info_message = (Button) findViewById(R.id.user_info_message);
		user_info_add_blacklist = (Button) findViewById(R.id.user_info_add_blacklist);
		user_info_manage_blacklist = (Button) findViewById(R.id.user_info_manage_blacklist);
		user_info_manage_blacklist.setOnClickListener(rbc);

		user_info_name = (TextView) findViewById(R.id.user_info_name);
		user_info_sex = (TextView) findViewById(R.id.user_info_sex);
		user_info_desc = (TextView) findViewById(R.id.user_info_desc);
		user_info_public_topic_counts = (TextView) findViewById(R.id.user_info_public_topic_counts);
		user_info_attention_counts = (TextView) findViewById(R.id.user_info_attention_counts);
		user_info_join_topic_counts = (TextView) findViewById(R.id.user_info_join_topic_counts);
		user_info_fans_counts = (TextView) findViewById(R.id.user_info_fans_counts);

		user_info_layout_publictopiccounts = (LinearLayout) findViewById(R.id.user_info_layout_publictopiccounts);
		user_info_layout_attentioncounts = (LinearLayout) findViewById(R.id.user_info_layout_attentioncounts);
		user_info_layout_jointopiccounts = (LinearLayout) findViewById(R.id.user_info_layout_jointopiccounts);
		user_info_layout_fanscounts = (LinearLayout) findViewById(R.id.user_info_layout_fanscounts);
		// if (curUserId == Constants.USER_ID) {// 别人
		user_info_layout_publictopiccounts.setOnClickListener(rbc);
		user_info_layout_attentioncounts.setOnClickListener(rbc);
		user_info_layout_jointopiccounts.setOnClickListener(rbc);
		user_info_layout_fanscounts.setOnClickListener(rbc);
		// }

	}

}
